package com.csu.tour.controller;

import com.csu.tour.common.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @PostMapping("/uploadMedia")
    public CommonResponse uploadMedia(MultipartFile media) throws Exception {
        log.info("media: {}", media);
        String originalFilename = media.getOriginalFilename();
        int index= originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(index);
        String newFilename = UUID.randomUUID().toString() + extname;
        log.info("newFilename: {}", newFilename);
        media.transferTo(new File("D:\\complaintMedia\\" + newFilename ));
        String url = "D:\\complaintMedia\\" + newFilename;
        return CommonResponse.createForSuccess(url);
    }
}
